#include <zmq.hpp>
#include <string>
#include <iostream>

int main() {
    // 创建 ZeroMQ 上下文和套接字
    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_REQ);

    // 连接到创建用户服务
    socket.connect("tcp://localhost:5555");

    // 向创建用户服务发送请求
    std::string message = "1127:NewUsercc"; // 假设要创建的用户信息为 "UserID:UserName"
    zmq::message_t request(message.size());
    memcpy(request.data(), message.data(), message.size());
    if (!socket.send(request, zmq::send_flags::none)) { // 检查send函数的返回值
        std::cerr << "Failed to send message to create user service" << std::endl;
        return 1;
    }

    // 接收并打印创建用户服务的响应
    zmq::message_t reply;
    if (!socket.recv(reply, zmq::recv_flags::none)) { // 检查recv函数的返回值
        std::cerr << "Failed to receive reply from create user service" << std::endl;
        return 1;
    }
    std::string replyMessage = std::string(static_cast<char*>(reply.data()), reply.size());
    std::cout << "Received reply from create user service: " << replyMessage << std::endl;

    // 关闭与创建用户服务的连接
    socket.disconnect("tcp://localhost:5555");

    // 连接到获取用户信息服务
    socket.connect("tcp://localhost:5556");

    // 向获取用户信息服务发送请求
    message = "1127"; // 假设要获取的用户ID为1127
    request = zmq::message_t(message.size());
    memcpy(request.data(), message.data(), message.size());
    if (!socket.send(request, zmq::send_flags::none)) { // 检查send函数的返回值
        std::cerr << "Failed to send message to get user info service" << std::endl;
        return 1;
    }

    // 接收并打印获取用户信息服务的响应
    if (!socket.recv(reply, zmq::recv_flags::none)) { // 检查recv函数的返回值
        std::cerr << "Failed to receive reply from get user info service" << std::endl;
        return 1;
    }
    replyMessage = std::string(static_cast<char*>(reply.data()), reply.size());
    std::cout << "Received reply from get user info service: " << replyMessage << std::endl;

    // 关闭套接字和上下文
    socket.close();
    context.close();

    return 0;
}